﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace objass
{
    /// <summary>
    /// NodeInput.xaml 的交互逻辑
    /// </summary>
    public partial class NodeInput : Window
    {
        public string SelectedWord { get; private set; }
        public void LoadNodes()
        {
            var app = (App)Application.Current;
            var nodes = app.GetNodeNames();
            // 清空 ListBox
            wordSelector.Items.Clear();
            // 遍历节点列表并添加到 ListBox
            foreach (var node in nodes)
            {
                wordSelector.Items.Add(node);
            }
        }

        public NodeInput()
        {
            InitializeComponent();
            LoadNodes();
        }
        private void NodeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // 处理 inputNode 的输入变化事件
            TextBox textBox = sender as TextBox;
            if (textBox != null)
            {
                string newText = textBox.Text;
                // WordSelector根据newText的值，定位到最接近的选项，并选中
                if (newText != string.Empty)
                {
                    for (int i = 0; i < wordSelector.Items.Count; i++)
                    {
                        if (wordSelector.Items[i].ToString().StartsWith(newText))
                        {
                            wordSelector.SelectedIndex = i;
                            wordSelector.ScrollIntoView(wordSelector.Items[i]); // 确保选中的项在可视化界面里
                            break;
                        }
                    }
                }
                else
                {
                    wordSelector.SelectedIndex = -1; // 清除选中项
                }
            }
        }
        private void WordSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void WordSelector_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // 处理 wordSelector 的鼠标左键释放事件
            ListBox listBox = sender as ListBox;
            if (listBox != null && listBox.SelectedItem != null)
            {
                string selectedItem = listBox.SelectedItem.ToString();
                NodeTextBox.Text = selectedItem;
            }
        }


        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            if(wordSelector.SelectedItem != null)
            {
                SelectedWord = wordSelector.SelectedItem.ToString();
            }
            else
            {
                SelectedWord = NodeTextBox.Text;
            }
            this.DialogResult = true;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
